package com.weiqiao.api.service.impl;

import com.google.gson.Gson;
import com.mingsoft.util.StringUtil;
import com.weiqiao.api.domain.StrategyApiEntity;
import com.weiqiao.api.domain.vo.StrategyApiVO;
import com.weiqiao.api.repository.IStrategyApiRepository;
import com.weiqiao.api.service.IStrategyApiService;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service("StrategyApiServiceImpl")
public class StrategyApiServiceImpl implements IStrategyApiService {

    @Autowired
    private IStrategyApiRepository strategy;

    @Override
    public List<StrategyApiEntity> strategyList(StrategyApiVO vo) {
        //Bson filter, int company_id
        Document doc = new Document(); // 查询条件
        doc.append("delete_flag", 0);
        doc.append("company_id", vo.getCompany_id());
        // 根据攻略id进行查询，查详情
        if (!StringUtil.isBlank(vo.getStrategy_id())) {
            doc.append("strategy_id", vo.getStrategy_id());
        }
        Document sort = new Document();
        sort.append("sort", 1);
        int pagesize = vo.getPagesize();
        int pagenum = vo.getPagenum() * pagesize;
        List<String> list = strategy.strategyList(doc, sort, pagenum, pagesize);
        List<StrategyApiEntity> result = new ArrayList<>();
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Gson gson = new Gson();
        list.forEach(s -> {
            StrategyApiEntity Entity = gson.fromJson(s, StrategyApiEntity.class);
            String time = Entity.getCreate_date();
            long create_time = Long.parseLong(time);
            Entity.setCreate_date(sd.format(new Date(create_time)));
            result.add(Entity);
        });
        return result;
    }
}
